﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using PiiSWebAPI.Models;

namespace PiiSWebAPI.Controllers
{
    public class MovieController : ApiController
    {
        private readonly PiiSDataModelContainer _db = new PiiSDataModelContainer();

        // GET api/Movie
        public IEnumerable<Movie> GetMovies()
        {
            return _db.MovieSet.AsEnumerable();
        }

        // GET api/Movie/5
        public IEnumerable<EvaluatedMovie> GetMovie(int id)
        {
            var movies = _db.MovieSet.AsEnumerable();
            var movieArr = movies as Movie[] ?? movies.ToArray();
            var moviesList = (from movie in movieArr
                              let eval = movie.Evaluation.FirstOrDefault(w => w.PersonId == id)
                              select new EvaluatedMovie
                                         {
                                             Id = movie.Id,
                                             TMDBId = movie.TMDBId,
                                             Title = movie.Title,
                                             Evaluation = eval == null ? -1 : eval.EvaluationValue == null ? null : eval.EvaluationValue+1
                                         });
            return moviesList;
        }

        //// PUT api/Movie/5
        //public HttpResponseMessage PutMovie(int id, Movie movie)
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        //    }

        //    if (id != movie.Id)
        //    {
        //        return Request.CreateResponse(HttpStatusCode.BadRequest);
        //    }

        //    db.Entry(movie).State = EntityState.Modified;

        //    try
        //    {
        //        db.SaveChanges();
        //    }
        //    catch (DbUpdateConcurrencyException ex)
        //    {
        //        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
        //    }

        //    return Request.CreateResponse(HttpStatusCode.OK);
        //}

        //// POST api/Movie
        //public HttpResponseMessage PostMovie(Movie movie)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.MovieSet.Add(movie);
        //        db.SaveChanges();

        //        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, movie);
        //        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = movie.Id }));
        //        return response;
        //    }
        //    else
        //    {
        //        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        //    }
        //}

        //// DELETE api/Movie/5
        //public HttpResponseMessage DeleteMovie(int id)
        //{
        //    Movie movie = db.MovieSet.Find(id);
        //    if (movie == null)
        //    {
        //        return Request.CreateResponse(HttpStatusCode.NotFound);
        //    }

        //    db.MovieSet.Remove(movie);

        //    try
        //    {
        //        db.SaveChanges();
        //    }
        //    catch (DbUpdateConcurrencyException ex)
        //    {
        //        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
        //    }

        //    return Request.CreateResponse(HttpStatusCode.OK, movie);
        //}

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}